package com.lineage.server.command.executor;

import com.lineage.server.datatables.ExpTable;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.RangeInt;
import com.lineage.server.world.World;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1LevelToPc implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1LevelToPc.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1LevelToPc();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      StringTokenizer tok = new StringTokenizer(arg);
      String char_name = tok.nextToken();
      int level = Integer.parseInt(tok.nextToken());
      L1PcInstance target = World.get().getPlayer(char_name);
      if (target == null) {
        if (pc == null)
          return; 
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(73, char_name));
        return;
      } 
      if (level == pc.getLevel()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, String.valueOf(String.valueOf(char_name)) + "当前已经是" + level + "级"));
        return;
      } 
      if (!RangeInt.includes(level, 1, ExpTable.MAX_LEVEL)) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("范围限制 1~" + ExpTable.MAX_LEVEL));
        return;
      } 
      long nowexp = target.getExp();
      long levelexp = ExpTable.getExpByLevel(level);
      long add = levelexp - nowexp;
      target.setExp(ExpTable.getExpByLevel(level));
      target.onChangeExp();
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, String.valueOf(String.valueOf(char_name)) + "的等级已经变更为:" + level + "级"));
    } catch (Exception e) {
      _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
    } 
  }
}
